<template>
  <div >
    <van-nav-bar
  title="收货地址"

  left-arrow
  @click-left="back"
/>

<van-address-list
v-if="list"
  v-model="chosenAddressId"
  :list="list"
  default-tag-text="默认"
  @add="onAdd"
  @edit="onEdit"
  @select="select"
/>
  </div>
</template>

<script>

import { Toast } from 'vant'
import { getAdress } from '@/api/pay'

export default {
  data () {
    return {
      chosenAddressId: '1',
      list: [

      ]
    }
  },
  methods: {
    onAdd () {
      Toast('新增地址')
      this.$router.push('/addAddress')
    },
    onEdit (item, index) {
      Toast('编辑地址:' + index)
    },
    async getAdressData () {
      const res = await getAdress()
      console.log(res)
      res.data.list.forEach(item => {
        this.list.push({
          id: item.address_id,
          name: item.name,
          tel: item.phone,
          address: item.region.province + item.region.city + item.region.region + item.detail,
          isDefault: item.address_id === this.selectId
        })
      })
      // console.log(this.addressList[0])
      this.chosenAddressId = this.selectId
    },
    select (item) {
      console.log(item.id)
      this.$router.replace(this.$route.query.backUrl || '/pay')
      this.$store.commit('user/setAddress', item)
    },
    back () {
      this.$router.replace(this.$route.query.backUrl || '/pay')
    }
  },
  created () {
    this.getAdressData()
    console.log(typeof this.selectId)
  },
  computed: {
    selectId () {
      return +this.$route.query.selectId
    }
  }
}

</script>

<style>

</style>
